#pragma once
#include <atlstr.h>
#include <string.h>
#include "..\tracedll\message.h"
#include <list>
using namespace std;
interface StatusLogger{
	virtual void StatusUpdate(const TCHAR *message) = 0;
};
class TraceServer
{
public:
	struct Client : OVERLAPPED
	{
		HANDLE          hPipe;
		BOOL            fAwaitingAccept;
		LogMessage		messageBuf[BUF_SIZE];
	};
	static TraceServer* GetInstance();
	static void Destory();
	BOOL Init();
	BOOL Attach(DWORD PID);
	static DWORD WINAPI WorkerThread(LPVOID p);
	DWORD WorkerThreadFunc();
	BOOL CreatePipe();
	BOOL CloseConnection(Client *pClient);
	BOOL CreateWorkerThread();
	BOOL SaveLog(FILE *fp);
	void SetStatusLogger(StatusLogger *statusLogger){
		m_statusLogger = statusLogger;
	}
	~TraceServer(){
		m_statusLogger = NULL;
	}
private:

	CRITICAL_SECTION m_rawMessageQueueLock;
	

	HANDLE m_hCompletionPort;

	StatusLogger *m_statusLogger;
	static TraceServer *s_instance;
	void StatusUpdate(const TCHAR *message);
	TraceServer(){m_statusLogger = NULL;}
}; 
